package com.feicheng.lanniao.controller.pc;

import java.beans.Transient;
import java.util.HashMap;
import java.util.Map;

import com.feicheng.lanniao.service.QuotaRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.feicheng.lanniao.entity.Client;
import com.feicheng.lanniao.entity.Person;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IClientPersonVoService;
import com.feicheng.lanniao.service.IClientService;
import com.feicheng.lanniao.service.IPersonService;
import com.tencentcloudapi.cdb.v20170320.models.ModifyTimeWindowRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/lanniao-user/client")
public class ClientPcController {
	@Autowired
	QuotaRecordService quotaRecordService;
	@Autowired
	IClientService IClientService1;
	@Autowired
	IPersonService IPersonService;

	/**
	 * 修改一条客户信息
	 * @param client 账户对象
	 * @param person 客户信息对象
	 * @return
	 */
	@PutMapping("/info")
	@Transient
	public ResultInfo updateClient(Client client, Person person) {
		System.out.println(client);
		System.out.println(person);
		QueryWrapper<Client> queryWrapper = new QueryWrapper<Client>();
		queryWrapper.eq("person_id", client.getPersonId());
		IClientService1.update(client, queryWrapper);
		QueryWrapper<Person> queryWrapper2 = new QueryWrapper<Person>();
		queryWrapper2.eq("person_id", person.getPersonId());
		IPersonService.update(person,queryWrapper2);
		return ResultInfo.success();
	}
	@Autowired
	private IClientPersonVoService iClientPersonVoService;
	/**
	 * 查询信息
	 * @param currentPage
	 * @return boolean值
	 */
	@GetMapping("/page")
	public ResultInfo selectInfo(int currentPage) {
		return iClientPersonVoService.selectInfo(currentPage);
	}

	/**
	 * 根据id查询
	 * @param clientId
	 * @return map集合
	 */
	@GetMapping("/info")
	public ResultInfo selectOne(int clientId) {
		QueryWrapper<Client> queryWrapper = new QueryWrapper<Client>();
		QueryWrapper<Person> queryWrapper1 = new QueryWrapper<Person>();
		queryWrapper.eq("client_id", clientId);
		Client client  = IClientService1.getOne(queryWrapper);		
		queryWrapper1.eq("person_id", client.getPersonId());
		Person person = IPersonService.getOne(queryWrapper1);		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("client", client);
		map.put("person", person);		
		return ResultInfo.success(map);
	}

	@PostMapping("/info")
	@Transactional
	public ResultInfo addpeResultInfo(String account, String password, String name, String tel,
									  String secId) {
		Person person = new Person();
		person.setPersonName(name);
		person.setPhone(tel);
		person.setIdentityCard(secId);
		QueryWrapper<Person> perqQueryWrapper = new QueryWrapper<Person>();
		perqQueryWrapper.eq("phone", tel);
		Person person1 = IPersonService.getOne(perqQueryWrapper);
		if(null != person1) {
			throw new SystemException("手机号已存在");
		}
		IPersonService.addPersonInfo(person);
		QueryWrapper<Person> queryWrapper = new QueryWrapper<Person>();
		queryWrapper.eq("phone", person.getPhone());
		Person person2 = IPersonService.getOne(queryWrapper);
		Client client = new Client();
		client.setAccount(account);
		client.setPassword(password);
		client.setDeleteFlag(true);
		client.setPersonId(person2.getPersonId());
		IClientService1.addclienInfo(client);
		Client client1 = IClientService1.getById(person2.getPersonId());
		quotaRecordService.initQuota(client1.getClientId());
		return ResultInfo.success();
	}
}
